package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.business.vo.SearchAllVO;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;

@RestController
public class SearchController {

    @Autowired
    private ISearchService searchService;




    @GetMapping("/search")
    public R<?> search(SearchQuery qo) throws UnsupportedEncodingException, InvocationTargetException, IllegalAccessException {
        String kw = URLDecoder.decode(qo.getKeyword(), "utf-8");
        qo.setKeyword(kw);

        switch (qo.getType()){
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUser(qo));
            case 4:
                return R.ok(searchAnswer(qo));
            default:
                return R.ok(searchAll(qo));
        }

//        System.err.println(qo.getKeyword());
//        System.err.println(qo);
//        return R.ok();
    }

    private Object searchAll(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<Strategy> strategyPage = searchService.searchHighLight(Strategy.class, StrategyEs.class, qo, "title", "subTitle", "summary");
        Page<Note> notePage = searchService.searchHighLight(Note.class, NoteEs.class, qo, "title", "summary");
        Page<UserInfo> userInfoPage = searchService.searchHighLight(UserInfo.class, UserInfoEs.class, qo, "info", "nickname", "city");
        Page<TaDestination> destinationPage = searchService.searchHighLight(TaDestination.class, TaDestinationEs.class, qo, "name", "info");
        SearchAllVO vo = new SearchAllVO();
        vo.setDests(destinationPage.getContent());
        vo.setStrategies(strategyPage.getContent());
        vo.setNotes(notePage.getContent());
        vo.setUsers(userInfoPage.getContent());
        vo.setTotal(strategyPage.getTotalElements()  + notePage.getTotalElements() + destinationPage.getTotalElements() + userInfoPage.getTotalElements());
        return vo;
    }

    private Object searchAnswer(SearchQuery qo) {
        return R.ok();
    }

    private Object searchUser(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchHighLight(UserInfo.class, UserInfoEs.class,qo,"info","nickname","city");
    }

    private Object searchNote(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchHighLight(Note.class, NoteEs.class,qo,"title","summary");
    }

    private Object searchStrategy(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<Strategy> strategies = searchService.searchHighLight(Strategy.class, StrategyEs.class, qo, "title", "subTitle", "summary");
//        searchService.searchHighLight(StrategyEs.INDEX_NAME,StrategyEs.class,qo,"title","subTitle","summary");
//        searchService.searchHighLight(StrategyEs.INDEX_NAME,Strategy.class,qo,"title","subTitle","summary");
        return strategies;
    }

}
